﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace TARS.AdminCP
{
    public partial class Delivery : System.Web.UI.Page
    {
        TARSDelivery_EprojectEntities db = new TARSDelivery_EprojectEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!(Page.IsPostBack))
            {
                var getBranchesZone = db.t_BrachesZone.Where(x => x.Active == true);

                RadComboBoxItem comboboxitem = new RadComboBoxItem();
                comboboxitem.Text = "Select Branches Zone";
                comboboxitem.Value = "All";
                cboFilterBranchesZone.Items.Add(comboboxitem);

                foreach (var get in getBranchesZone)
                {
                    comboboxitem = new RadComboBoxItem();
                    comboboxitem.Text = get.ZoneName;
                    comboboxitem.Value = get.ID.ToString();
                    cboFilterBranchesZone.Items.Add(comboboxitem);
                }
            }
            else
            {
                if (cboFilterBranchesZone.SelectedValue != "" && cboFilterCarrierTypeName.SelectedValue != "" && cboFilterBranchesZone.SelectedValue != "All" && cboFilterCarrierTypeName.SelectedValue != "All")
                {
                    Guid BranchesZoneId = new Guid(cboFilterBranchesZone.SelectedItem.Value);
                    Guid CarrierTypeID = new Guid(cboFilterCarrierTypeName.SelectedItem.Value);
                    SqlDataSource3.SelectCommand = "SELECT * FROM [v_DeliveryManager] Where BranchesID = '" + BranchesZoneId + "' and CarrierTypeID = '" + CarrierTypeID + "'";

                }
                if (DeliveryInfoPnael.Visible == true)
                {
                    Guid DeliveryMasterID = new Guid(Session["DeliveryMasterID"].ToString());
                    var getDeliveryStatus = db.t_DeliveryStatus.Where(x => x.DeliveryMasterID == DeliveryMasterID).OrderByDescending(x => x.CreatedTime);
                    RadGrid2.DataSource = getDeliveryStatus.ToList();
                }
            }
        }

        protected void cboFilterBranchesZone_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            btnViewDelivery.Visible = false;
            RadGrid1.Visible = false;
            DeliveryInfoPnael.Visible = false;
            btnShow.Enabled = false;
            cboFilterCarrierTypeName.Text = "";
            cboFilterCarrierTypeName.Items.Clear();
            if (e.Value != "All")
            {
                cboFilterCarrierTypeName.Enabled = true;

                Guid BranchesZoneID = new Guid(e.Value);
                var getCarrierType = db.t_CarrierType.Where(x => x.Active == true && x.BranchesZoneID == BranchesZoneID);

                RadComboBoxItem comboboxitem = new RadComboBoxItem();
                comboboxitem.Text = "Select Carrier Type";
                comboboxitem.Value = "All";
                cboFilterCarrierTypeName.Items.Add(comboboxitem);

                foreach (var get in getCarrierType)
                {
                    comboboxitem = new RadComboBoxItem();
                    comboboxitem.Text = get.CarrierTypeName;
                    comboboxitem.Value = get.ID.ToString();
                    cboFilterCarrierTypeName.Items.Add(comboboxitem);
                }
            }
            else
            {
                cboFilterCarrierTypeName.Enabled = false;

            }
        }

        protected void cboFilterCarrierTypeName_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            btnViewDelivery.Visible = false;
            RadGrid1.Visible = false;
            DeliveryInfoPnael.Visible = false;
            if (e.Value != "All")
                btnShow.Enabled = true;
            else
                btnShow.Enabled = false;
        }

        protected void btnShow_Click(object sender, EventArgs e)
        {
            btnViewDelivery.Visible = true;
            RadGrid1.Visible = true;
            Guid BranchesZoneId = new Guid(cboFilterBranchesZone.SelectedItem.Value);
            Guid CarrierTypeID = new Guid(cboFilterCarrierTypeName.SelectedItem.Value);
            SqlDataSource3.SelectCommand = "SELECT * FROM [v_DeliveryManager] Where BranchesID = '" + BranchesZoneId + "' and CarrierTypeID = '" + CarrierTypeID + "'";

        }

        protected void btnViewDelivery_Click(object sender, EventArgs e)
        {
            if (RadGrid1.SelectedValue != null)
            {
                DeliveryInfoPnael.Visible = true;
                Session["DeliveryMasterID"] = RadGrid1.SelectedValues["NewID"].ToString();
                LoadDeliveryInfo();

                Guid DeliveryMasterID = new Guid(Session["DeliveryMasterID"].ToString());
                var getDeliveryStatus = db.t_DeliveryStatus.Where(x => x.DeliveryMasterID == DeliveryMasterID).OrderByDescending(x => x.CreatedTime);
                RadGrid2.DataSource = getDeliveryStatus.ToList();
                RadGrid2.DataBind();
            }
        }

        private void LoadDeliveryInfo()
        {
            Guid DeliveryMasterID = new Guid(Session["DeliveryMasterID"].ToString());
            var getDelivery = db.t_DeliveryMaster.First(x => x.ID == DeliveryMasterID);

            txtCustomerFName.Text = getDelivery.Customer_FName.ToString();
            txtCustomerLName.Text = getDelivery.Customer_LName.ToString();
            txtCustomerAddress.Text = getDelivery.Customer_Address.ToString();
            txtCustomerCity.Text = getDelivery.Customer_City.ToString();
            txtCustomerState.Text = getDelivery.Customer_State.ToString();
            txtCustomerZipCode.Text = getDelivery.Customer_ZipCode.ToString();
            txtCustomerPhoneNumber.Text = getDelivery.Customer_ContactNumber.ToString();
            txtCustomerEmail.Text = getDelivery.Customer_Email.ToString();

            txtShipFName.Text = getDelivery.Delivery_FName.ToString();
            txtShipLName.Text = getDelivery.Delivery_LName.ToString();
            txtShipAddress.Text = getDelivery.Delivery_Address.ToString();
            txtShipCity.Text = getDelivery.Delivery_City.ToString();
            txtShipState.Text = getDelivery.Delivery_State.ToString();
            txtShipZipCode.Text = getDelivery.Delivery_ZipCode.ToString();
            txtShipPhone.Text = getDelivery.Delivery_ContactNumber.ToString();

            txtTotalWeight.Text = getDelivery.TotalWeight.ToString();
            txtTotalMoney.Text = getDelivery.TotalMoney.ToString();
            txtScretNumber.Text = getDelivery.ScretNumber.ToString();
            txtEmpName.Text = getDelivery.t_EmployeeInfo.Emp_FName.ToString() + " " + getDelivery.t_EmployeeInfo.Emp_LName.ToString();
            txtItemDetails.Text = getDelivery.ItemDetails;
            if (getDelivery.Active == true)
            {
                btnNo.Checked = false;
                btnYes.Checked = true;
            }
            else
            {
                btnYes.Checked = false;
                btnNo.Checked = true;
            }
            txtCreatedTime.Text = getDelivery.CreatedTime.ToString();
        }

        protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {
        }

        protected void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
            {
                //Set Visible in Insert & Update Item
                GridEditFormItem edit = (GridEditFormItem)e.Item;
                edit["ID"].Visible = false;
                edit["DeliveryMasterID"].Visible = false;
                edit["CreatedTime"].Visible = false;
            }
        }

        protected void RadGrid2_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.EditCommandName)
                RadGrid2.MasterTableView.IsItemInserted = false; //Close Insert Item if Update Item Open
            if (e.CommandName == RadGrid.InitInsertCommandName)
                RadGrid2.MasterTableView.ClearEditItems(); //Close Update Item if Insert Item Open
        }

        protected void RadGrid2_InsertCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem item = (GridEditableItem)e.Item;
            String DeliveryStatus = (item["DeliveryStatus"].Controls[0] as TextBox).Text;

            Guid ID = Guid.NewGuid();
            Guid DeliveryMasterID = new Guid(Session["DeliveryMasterID"].ToString());
            t_DeliveryStatus deliverystatus = new t_DeliveryStatus();
            deliverystatus.ID = ID;
            deliverystatus.CreatedTime = DateTime.Now;
            deliverystatus.DeliveryMasterID = DeliveryMasterID;
            deliverystatus.DeliveryStatus = DeliveryStatus;
            db.t_DeliveryStatus.Add(deliverystatus);
            db.SaveChanges();

            var stat = db.t_DeliveryStatus.Where(x => x.DeliveryMasterID == DeliveryMasterID).OrderByDescending(x => x.CreatedTime);
            RadGrid2.DataSource = stat.ToList();

            RadGrid2.MasterTableView.ClearEditItems();
            RadGrid2.MasterTableView.ClearSelectedItems();
            RadGrid2.MasterTableView.ClearChildEditItems();
        }

        protected void RadGrid2_UpdateCommand(object sender, GridCommandEventArgs e)
        {

            Guid DeliveryMasterID = new Guid(Session["DeliveryMasterID"].ToString());
            GridEditableItem item = (GridEditableItem)e.Item;
            String DeliveryStatus = (item["DeliveryStatus"].Controls[0] as TextBox).Text;
            String ID = (item["ID"].Controls[0] as TextBox).Text;
            Guid DeliveryStatusID = new Guid(ID);
            t_DeliveryStatus edit = db.t_DeliveryStatus.First(x => x.ID == DeliveryStatusID);
            edit.DeliveryStatus = DeliveryStatus;
            db.SaveChanges();

            var stat = db.t_DeliveryStatus.Where(x => x.DeliveryMasterID == DeliveryMasterID).OrderByDescending(x => x.CreatedTime);
            RadGrid2.DataSource = stat.ToList();

            RadGrid2.MasterTableView.ClearEditItems();
            RadGrid2.MasterTableView.ClearSelectedItems();
            RadGrid2.MasterTableView.ClearChildEditItems();
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            Guid DeliveryMasterID = new Guid(Session["DeliveryMasterID"].ToString());
            t_DeliveryMaster getDelivery = db.t_DeliveryMaster.First(x => x.ID == DeliveryMasterID);

            getDelivery.Customer_FName = txtCustomerFName.Text;
            getDelivery.Customer_LName = txtCustomerLName.Text;
            getDelivery.Customer_Address = txtCustomerAddress.Text;
            getDelivery.Customer_City = txtCustomerCity.Text;
            getDelivery.Customer_State = txtCustomerState.Text;
            getDelivery.Customer_ZipCode = txtCustomerZipCode.Text;
            getDelivery.Customer_ContactNumber = txtCustomerPhoneNumber.Text;
            getDelivery.Customer_Email = txtCustomerEmail.Text;

            getDelivery.Delivery_FName = txtShipFName.Text;
            getDelivery.Delivery_LName = txtShipLName.Text;
            getDelivery.Delivery_Address = txtShipAddress.Text;
            getDelivery.Delivery_City = txtShipCity.Text;
            getDelivery.Delivery_State = txtShipState.Text;
            getDelivery.Delivery_ZipCode = txtShipZipCode.Text;
            getDelivery.Delivery_ContactNumber = txtShipPhone.Text;

            getDelivery.ItemDetails = txtItemDetails.Text;
            bool Active;
            if (btnYes.Checked == true)
                Active = true;
            else
                Active = false;
            getDelivery.Active = Active;

            DeliveryInfoPnael.Visible = false;
            Guid BranchesZoneId = new Guid(cboFilterBranchesZone.SelectedItem.Value);
            Guid CarrierTypeID = new Guid(cboFilterCarrierTypeName.SelectedItem.Value);
            SqlDataSource3.SelectCommand = "SELECT * FROM [v_DeliveryManager] Where BranchesID = '" + BranchesZoneId + "' and CarrierTypeID = '" + CarrierTypeID + "'";
            db.SaveChanges();
        }

    }
}